package com.xindaoapp.henghua.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.xindaoapp.henghua.db.model.PropertyDao;
import com.xindaoapp.henghua.db.model.PropertyWithLabelDao;
import com.xindaoapp.henghua.db.model.ScanDatasDao;
import com.xindaoapp.henghua.db.model.SupervisionDao;
import com.xindaoapp.henghua.db.model.TagsDao;
import com.xindaoapp.henghua.db.model.TransportDatasDao;

import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;

public class DaoMaster extends AbstractDaoMaster {
	public static final int SCHEMA_VERSION = 3;

	/** Creates underlying database table using DAOs. */
	public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {
		PropertyDao.createTable(db, ifNotExists);
		PropertyWithLabelDao.createTable(db, ifNotExists);
		ScanDatasDao.createTable(db, ifNotExists);
		SupervisionDao.createTable(db, ifNotExists);
		TransportDatasDao.createTable(db, ifNotExists);
		TagsDao.createTable(db, ifNotExists);
	}

	/** Drops underlying database table using DAOs. */
	public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {
		PropertyDao.dropTable(db, ifExists);
		PropertyWithLabelDao.dropTable(db, ifExists);
		ScanDatasDao.dropTable(db, ifExists);
		SupervisionDao.dropTable(db, ifExists);
		TransportDatasDao.dropTable(db, ifExists);
		TagsDao.dropTable(db, ifExists);
	}

	public static abstract class OpenHelper extends SQLiteOpenHelper {
		public OpenHelper(Context context, String name, CursorFactory factory) {
			super(context, name, factory, SCHEMA_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			createAllTables(db, false);
		}

	}

	/** WARNING: Drops all table on Upgrade! Use only during development. */
	public static class DevOpenHelper extends OpenHelper {

		public DevOpenHelper(Context context, String name, CursorFactory factory) {
			super(context, name, factory);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			dropAllTables(db, false);
		}

	}

	public DaoMaster(SQLiteDatabase db) {
		super(db, SCHEMA_VERSION);
		registerDaoClass(PropertyDao.class);
		registerDaoClass(PropertyWithLabelDao.class);
		registerDaoClass(ScanDatasDao.class);
		registerDaoClass(SupervisionDao.class);
		registerDaoClass(TransportDatasDao.class);
		registerDaoClass(TagsDao.class);
	}

	public DaoMaster(SQLiteDatabase db, int schemaVersion) {
		super(db, schemaVersion);
	}

	@Override
	public DaoSession newSession() {
		return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
	}

	@Override
	public DaoSession newSession(IdentityScopeType type) {
		return new DaoSession(db, type, daoConfigMap);
	}
}
